$(function () {
    /*项目路径*/
    var baseURL = /knowledge/
    /*构建列表*/
    $("#jqGrid").jqGrid({
        url: baseURL + '/document/knowledgeReview/wdsh',
        datatype: "json",
        colModel: [
            {label: '编号', name: 'documentId', index: "documentOrigin", width: 20, key: true},
            {label: '来源', name: 'documentOrigin', index: "documentOrigin", width: 45},
            {label: '作者', name: 'documentAuthor', width: 35},
            {label: '时间', name: 'documentUploadDate', width: 90},
            {label: '摘要', name: 'documentAbstract', width: 100},
            {
                label: '审批状态', name: 'documentState', width: 30, formatter: function (value, options, row) {

                    if (value == 0) {
                        return '<span class="label label-info">等待审核</span>';
                    }
                    if (value == 1) {
                        return '<span class="label label-success">审核通过</span>';
                    }
                    if (value == 2) {
                        return '<span class="label label-danger">审核不过</span>';
                    }
                }
            },
            {
                label: '文档', name: 'files', width: 100, formatter: function (value, options, row) {
                    var spanItem = '<div id="aitemDiv">'
                    	
                    //if(value.length>1){
                    //	spanItem= '<div style="height:20px;overflow:hidden;">'
                   // }<a style="display:block;height:20px;width:20px;background:url(../images/down.png) ;background-repeat:no-repeat;background-size:contain;"></a>
                    for (var i = 0; i < value.length; i++) {
                        debugger
						console.log(value[i].documentSaveurl)
						var url = value[i].documentSaveurl
						
						var inputFile=url
						var newUrl = url.replace(/\\/g,"\/");
						var realUrl = baseURL + "localFile"+ newUrl.substr(12)
						var aItm=null;
						var index = realUrl.lastIndexOf(".");
						var ext = realUrl.substr(index+1).toLowerCase();
						var fileName=inputFile.split("\\")[3].substr(15)
						realUrl = encodeURI(realUrl)
						
						inputFile=encodeURI(inputFile)
						if(ext=="pdf"||ext=="txt"){//pdf txt 直接利用浏览器预览
							aItm = "<a class='label label-info'  style='display:block;margin-bottom:2px;'  target='_blank' href="+ realUrl + " title="+fileName+" >"+fileName+" </a>"
						}else{
							var flag=function(ext){
								if(ext=="pdf"||ext=="doc"||ext=="docx"||ext=="ppt"||ext=="pptx"||ext=="xlsx"||ext=="xls"){
									return true;
								}else{
									return false;
								}
							}
							if(flag(ext)){
								aItm = "<a class='label label-info' style='display:block;margin-bottom:2px;' documentId="+row.documentId+" inputFile="+inputFile+" id='officetopdf' title="+fileName+" >"+fileName+"</a>"
							}else{
								aItm = "<a class='label label-info' style='display:block;margin-bottom:2px;' documentId="+row.documentId+" inputFile="+inputFile+" id='downLoadFile' title="+fileName+" >"+fileName+"</a>"
							}

						}
						spanItem += aItm
                    }
                    spanItem + '</div>'
                    return spanItem;
                }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: false,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.pageNum",
            total: "page.pages",
            records: "page.total"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            
            //修改样式 start ==========================
            var divs=$("td").filter("[role=gridcell]").find("div")
            for(var i=0;i<divs.length;i++){
            	//console.log("itmes: "+divs[i].childElementCount)
            	lineCount=divs[i].childElementCount;
            	if(lineCount>1){
            		var tds=$(divs[i].parentElement.parentElement).find("td")
            		for(var j=0;j<tds.length-1;j++){
            			debugger
            			$(tds[j]).css({"padding-top":""+lineCount+"0px"})
            		}
            	}
            }
            //修改样式 end ==========================
        }
    });
    
    
    $(document).on("click","a[id='officetopdf']",function(){
    	var athis=$(this)
    	var documentId=parseInt(athis.attr("documentId"))
    	var inputFile=athis.attr("inputFile")
    	var url=baseURL+"lookdocument/officeToPdf"
    	debugger
    	var data={"inputFile":inputFile,"documentId":documentId}
    	var newwindow=window.open("about:blank");
    	$.post(url,data,function(r){
    		debugger;
    		console.log(r)
    		var url=r.url
    		var newUrl = url.replace(/\\/g,"\/");
			var realUrl = baseURL + "localFile"+ newUrl.substr(17)
			newwindow.location.href=realUrl;
            newwindow.focus();
    	});
    })
    $(document).on("click","a[id='downLoadFile']",function(){
    	var athis=$(this)
    	var fileName=athis.attr("title")
    	var filePath=athis.attr("inputFile")
    	var url=baseURL+"filedownload/download"
    	function download(){
        	var action=url;
    		var form = $("<form></form>")
    	    	form.attr({'action':action,'method':'post'})
    	    var input1 = $("<input type='hidden' name='filePath'/>")
    	     	input1.attr('value',filePath)
    	 	var input2 = $("<input type='hidden' name='fileName'/>")
    	 		input2.attr('value',fileName)
    	    	form.append(input1)
    	    	form.append(input2)
    	    	form.appendTo("body")//如果不添加到 body中是不会提交到后台
    	    	form.css('display','none')
    	    	form.submit()
    	}
    	download()
    })
})
var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            key: null
        },
        showList: true,
        title: null,
        document: {
            documentUploadDate: null,
            documentAbstract: null,
            documentId:null,
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'key': vm.q.key},
                page: page
            }).trigger("reloadGrid");
        },
        check: function () {
            var documentId = getSelectedRow();
            if (documentId == null) {
                return;
            }
            vm.showList = false;
            vm.title = "文档审批";
            //获取角色信息
            this.getDocumentById(documentId);
            vm.document.documentId=documentId;
        },
        getDocumentById:function (documentId) {
            $.get(baseURL + "document/getDocumentById/"+documentId, function(r){
                debugger
                vm.document = r.kwDocument;
            });
        },
        passOrFalse:function () {
            if(event){
                var status=event.target.getAttribute("status")
                debugger
            }
            var msg=""
            if(status==1){
                msg='确定要审核通过？'
            }else{
                msg='确定要审核不过？'
            }
            confirm(msg, function(){
                $.post(baseURL+"/document/passOrfailService",{"documentid":vm.document.documentId,"status":status},function(result){
                    alert("操作成功");
                    vm.reload();
                });
            });
    }
        }
});